package com.zyzd.greendaodemo.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.zyzd.greendaodemo.entity.UserInfo;

import com.zyzd.greendaodemo.entity.SportInfo;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "SPORT_INFO".
*/
public class SportInfoDao extends AbstractDao<SportInfo, Long> {

    public static final String TABLENAME = "SPORT_INFO";

    /**
     * Properties of entity SportInfo.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property SportId = new Property(0, Long.class, "sportId", true, "_id");
        public final static Property Date = new Property(1, String.class, "date", false, "DATE");
        public final static Property UserId = new Property(2, Long.class, "UserId", false, "USER_ID");
    }

    private DaoSession daoSession;

    private Query<SportInfo> userInfo_SportInfoQuery;

    public SportInfoDao(DaoConfig config) {
        super(config);
    }
    
    public SportInfoDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"SPORT_INFO\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: sportId
                "\"DATE\" TEXT," + // 1: date
                "\"USER_ID\" INTEGER);"); // 2: UserId
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"SPORT_INFO\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, SportInfo entity) {
        stmt.clearBindings();
 
        Long sportId = entity.getSportId();
        if (sportId != null) {
            stmt.bindLong(1, sportId);
        }
 
        String date = entity.getDate();
        if (date != null) {
            stmt.bindString(2, date);
        }
 
        Long UserId = entity.getUserId();
        if (UserId != null) {
            stmt.bindLong(3, UserId);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, SportInfo entity) {
        stmt.clearBindings();
 
        Long sportId = entity.getSportId();
        if (sportId != null) {
            stmt.bindLong(1, sportId);
        }
 
        String date = entity.getDate();
        if (date != null) {
            stmt.bindString(2, date);
        }
 
        Long UserId = entity.getUserId();
        if (UserId != null) {
            stmt.bindLong(3, UserId);
        }
    }

    @Override
    protected final void attachEntity(SportInfo entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public SportInfo readEntity(Cursor cursor, int offset) {
        SportInfo entity = new SportInfo( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // sportId
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // date
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2) // UserId
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, SportInfo entity, int offset) {
        entity.setSportId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setDate(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setUserId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(SportInfo entity, long rowId) {
        entity.setSportId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(SportInfo entity) {
        if(entity != null) {
            return entity.getSportId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(SportInfo entity) {
        return entity.getSportId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "sportInfo" to-many relationship of UserInfo. */
    public List<SportInfo> _queryUserInfo_SportInfo(Long sportId) {
        synchronized (this) {
            if (userInfo_SportInfoQuery == null) {
                QueryBuilder<SportInfo> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.SportId.eq(null));
                userInfo_SportInfoQuery = queryBuilder.build();
            }
        }
        Query<SportInfo> query = userInfo_SportInfoQuery.forCurrentThread();
        query.setParameter(0, sportId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserInfoDao().getAllColumns());
            builder.append(" FROM SPORT_INFO T");
            builder.append(" LEFT JOIN USER_INFO T0 ON T.\"USER_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected SportInfo loadCurrentDeep(Cursor cursor, boolean lock) {
        SportInfo entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        UserInfo userInfo = loadCurrentOther(daoSession.getUserInfoDao(), cursor, offset);
        entity.setUserInfo(userInfo);

        return entity;    
    }

    public SportInfo loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<SportInfo> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<SportInfo> list = new ArrayList<SportInfo>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<SportInfo> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<SportInfo> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
